var app = new Vue({
    el: "#app",
    data: {
        title: "口算练习题",
        settings: {
            "total": 100,
            "max": 100,
            "carry": 20,
            "blank": 20,
            "random": 1,
            "add": 40,
            "sub": 40,
            "mul": 20,
            "div": 0,
            "addsub": 0,
            "addmul": 0,
            "adddiv": 0,
            "submul": 0,
            "subdiv": 0
        },
        warns: ["Done!"],
        questions: []
    },
    created: function(){
        this.rendit();
    },
    methods: {
        rendit: function(event){
            var _self = this;
            if(_self.settings.total > 156) _self.settings.total = 156;
            var _rand_num = function(_min, _max){
                return Math.floor(Math.random() * (_max - _min) + _min);
            }
            var _rand_add = function(){
                var _1st = _rand_num(1, _self.settings.max - 1);
                var _2ed = _rand_num(1, _self.settings.max - _1st);
                return [_1st, _2ed, _1st + _2ed, "+"];
            };
            var _rand_subtract = function(){
                var _as = _rand_add();
                return [_as[2], _as[1], _as[0], "-"];
            };
            var _rand_multiply = function(){
                var _1st = _rand_num(1, 9);
                var _2ed = _rand_num(1, 9);
                return [_1st, _2ed, _1st * _2ed, "*"];
            };
            var _rand_divide = function(){
                var _ms = _rand_multiply();
                return [_ms[2], _ms[1], _ms[0], "÷"];
            };
            var _rand_all = function(){
                var _funcs = [_rand_add, _rand_subtract, _rand_multiply, _rand_divide];
                var _choise = _rand_num(0, 3);
                return _funcs[_choise]();
            }
            var _fix_start = function(_num){
                if(_num < 10) return "&nbsp;" + _num;
                return _num;
            }
            var _fix_end = function(_num){
                if(_num < 10) return _num + "&nbsp;";
                return _num;
            }
            var _fmt_it = function(_p){
                return [_fix_start(_p[0]), _p[3], _fix_end(_p[1]), "=", "___"].join(" ");
            };
            _self.questions = [[],[],[],[]];
            for(var i=0; i < _self.settings.total; i++){
                _self.questions[i%4].push(_fmt_it(_rand_all()));
            }
        },
        printit: function(event){
            window.print();
        }
    }
});